<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><![endif]-->
  <title>天量科技管理后台</title>
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width">
  <link href="favicon.ico" rel="shortcut icon" type="image/x-icon">
  <link rel="stylesheet" href="css/templatemo_main.css">
  <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body ng-app="app" ng-controller="index">
<div class="navbar navbar-inverse" role="navigation">
  <div class="navbar-header">
    <div class="logo"><h1>天量科技管理后台</h1></div>
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
      <span class="sr-only">Toggle navigation</span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>
  </div>
</div>
<div class="template-page-wrapper">
  <div class="navbar-collapse collapse templatemo-sidebar">
    <ul class="templatemo-sidebar-menu">
      <li>
        <form class="navbar-form">
          欢迎您:{{user.name || '访客'}}</span>
        </form>
      </li>
      <li ng-repeat="menu in menus" ng-class="menu.menuclass" ng-click="menuclick(menu)">
        <a ng-click="redi(menu.url)" ng-if="menu.items.length==0"><i ng-class="menu.icon"></i>{{menu.label}}</a>
        <a href="#" ng-if="menu.items.length>0">
          <i ng-class="menu.icon"></i> {{menu.label}} <div class="pull-right"><span class="caret"></span></div>
        </a>
        <ul class="templatemo-submenu" ng-if="menu.items.length>0">
          <li ng-repeat="item in menu.items" ng-click="redi(item.url)"><a href="javascript:void(0)">{{item.label}}</a></li>
        </ul>
      </li>
      <li>
        <a href="#" data-toggle="modal" data-target="#confirmModal"><i class="fa fa-sign-out"></i>退出登录</a>
      </li>
    </ul>
  </div><!--/.navbar-collapse -->

  <div class="templatemo-content-wrapper">
    <div class="templatemo-content">
      <ol class="breadcrumb">
        <li><a ng-click="redi('index.html')" href="javascript:void(0)">首页</a></li>
        <li><a ng-click="redi('strategy_test.html')" href="javascript:void(0)">返回测试列表</a></li>
        <li><a ng-click="redi('strategy_test_param.html?id='+id)" href="javascript:void(0)">查看参数</a></li>
      </ol>
      <div class="row" ng-if="haserror">
        <div class="alert alert-danger alert-dismissible" role="alert">
          <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
          <strong>发生异常情况:{{errormessage}}</strong>
        </div>
      </div>


      <div class="row">
        <div class="col-md-3 margin-bottom-30">
          <label>策略名称:</label> {{data.strategyName}}
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>版版本:</label> {{data.version}}
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>测试用例名称:</label> {{data.name}}
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>测试用户:</label> {{data.userName}}
        </div>
      </div>
      <div class="row">
        <div class="col-md-3 margin-bottom-30">
          <label>测试起始时间:</label> {{data.startTime  | date: 'yyyy-MM-dd hh:mm:ss' }}
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>测试结束时间:</label> {{data.endTime  | date: 'yyyy-MM-dd hh:mm:ss'}}
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>回测开始日期:</label> {{data.beginDate | date: 'yyyy-MM-dd'}}
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>回测结束日期:</label> {{data.endDate | date: 'yyyy-MM-dd'}}
        </div>
      </div>
      <div class="row">
        <div class="col-md-3 margin-bottom-30">
          <label>初始金额:</label> {{data.initMoney }}
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>最后金额:</label> {{data.endMoney}}
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>总盈利率:</label> {{data.yields*100 | number : 2}}%
        </div>
        <div class="col-md-3 margin-bottom-30">
          <label>日盈利率:</label> {{data.yieldsPerDay*1000 | number : 2}}‰
        </div>
      </div>
      <div class="templatemo-chart-box col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div id="chart_daily" style="width:{{wWidth*0.8}}px;height:{{wHeigth*0.3}}px;"></div>
      </div>
      <div class="row" ng-repeat="symbol in symbols">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
          <div class="row templatemo-chart-row">
            <div class="templatemo-chart-box col-lg-12 col-md-12 col-sm-12 col-xs-12">
              <div id="chart_{{symbol}}" style="width:{{wWidth*0.8}}px;height:{{wHeigth*0.5}}px;"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- Modal -->
  <div class="modal fade" id="confirmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
          <h4 class="modal-title" id="myModalLabel">确定退出吗</h4>
        </div>
        <div class="modal-footer">
          <a ng-click="logout()" class="btn btn-primary" data-dismiss="modal">确定</a>
          <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        </div>
      </div>
    </div>
  </div>

  <footer class="templatemo-footer">
    <div class="templatemo-copyright">
      <p>Copyright &copy; 2018 By <a href="http://skyquant.cn" title="天量科技" target="_blank">天量科技</a></p>
    </div>
  </footer>
</div>
</body>

<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script src="//cdn.bootcss.com/angular.js/1.5.8/angular.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/echarts/4.1.0-release/echarts.min.js"></script>
<script src="js/common.js"></script>
<script src="js/chart.js"></script>
<script src="js/templatemo_script.js"></script>
<script type="text/javascript">
  'use strict';
  var app = angular.module('app', []);
  app.config(['$locationProvider', function ($locationProvider) {
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
  }]);
  var getScaSize = function(value){
    if(value[2]<0.004){
      return Math.round(4);
    }else if(value[2]<0.02){
      return Math.round(value[2]*1000);
    }else{
      return 20+Math.round(value[2]*100);
    }
  }
  function initchart(data){
    var dailychartdiv = document.getElementById('chart_daily');
    var dailymyChart = echarts.init(dailychartdiv);
    var option = {
        title: {
          text: '收益日K',
          left: 0
        },
        legend: {
            data: ['日K线']
        },
        tooltip: {
            trigger: 'item',
            axisPointer: {
                type: 'cross'
            }
        },
        xAxis: {
            type: 'category',
            data: data.dailyObj.map(function(item) {
              return item[0];
            }),
            axisLine: { lineStyle: { color: '#8392A5' } },
            splitNumber: 20,
            splitLine: {show: false},
        },
        yAxis: {
            scale: true,
            axisLine: { lineStyle: { color: '#8392A5' } },
            splitLine: { show: true },
            splitArea: {show: true }
        },
        dataZoom:
        {
            type: 'slider',
            show: false,
            yAxisIndex: [0],
            left: '96%',
            bottom: '5%',
            start: 0,
            end: 100
        },
        series:
          {
            type: 'candlestick',
            name: '日K线',
            data: data.dailyObj.map(function(item) {
              return [item[1], item[2], item[3], item[4]];
            }),
            itemStyle: {
                normal: {
                    color: upColor,
                    color0: downColor,
                    borderColor: '#FD1050',
                    borderColor0: '#0CF49B'
                }
            }
          }
    }
    dailymyChart.setOption(option);
    var charts =[];
    charts.push(dailymyChart);
    data.list.forEach(function(v,index){
      var chartdiv = document.getElementById('chart_'+v.symbol);
      var myChart = echarts.init(chartdiv);
      var option = {
        title: {
          text: v.symbol,
          left: 0
        },
        legend: {
            data: ['日K线','多单开单','多单止盈','多单止损','空单开单','空单止盈','空单止损']
        },
        tooltip: {
            trigger: 'item',
            axisPointer: {
                type: 'cross'
            },
            formatter: function(params) {
                 if(params.seriesName=='空单止盈' || params.seriesName=='多单止盈' || params.seriesName=='多单止损'|| params.seriesName=='空单止损'){
                    return '平仓价:'+params.value[1]
                    +'<br>仓量:'+params.value[2]
                    +'<br>盈亏:$'+params.value[3];
                 }else if(params.seriesName=='空单开单' || params.seriesName=='多单开单'){
                    return '开单价:'+params.value[1]
                    +'<br>仓量:'+params.value[2]
                    +'<br>止盈:'+params.value[3]
                    +'<br>止损:'+params.value[4];
                 }else if(params.seriesName=='日K线'){
                    return '开盘:'+params.value[1]
                        +'<br>收盘:'+params.value[2]
                        +'<br>最高:'+params.value[3]
                        +'<br>最低:'+params.value[4];
                 }
              }
        },
        xAxis: {
            type: 'category',
            data: data.dates,
            axisLine: { lineStyle: { color: '#8392A5' } },
            splitNumber: 20,
            splitLine: {show: false},
        },
        yAxis: {
            scale: true,
            axisLine: { lineStyle: { color: '#8392A5' } },
            splitLine: { show: true },
            splitArea: {show: true }
        },
        dataZoom: [
          {
              type: 'slider',
              show: false,
              yAxisIndex: [0],
              left: '96%',
              bottom: '5%',
              start: 0,
              end: 100
          },
            {
                show: true,
                type: 'slider',
                y: '90%',
                start: 0,
                end: 100
            }
          ],
        series: [
          {
            type: 'candlestick',
            name: '日K线',
            data: v.bars.map(function(item) {
              return [item[1], item[2], item[3], item[4]];
            }),
            itemStyle: {
                normal: {
                    color: upColor,
                    color0: downColor,
                    borderColor: '#FD1050',
                    borderColor0: '#0CF49B'
                }
            }
          },
          {
            name: '多单开单',
            type: 'scatter',
            symbolSize: getScaSize,
            color: '#1E90FF',
            data: v.buyopen,
          },
          {
            name: '多单止盈',
            type: 'scatter',
            symbolSize: getScaSize,
            symbol: 'image://../images/arrow/bl_u_32.png',
            data: v.buytpclose,
          },
          {
            name: '多单止损',
            type: 'scatter',
            symbol: 'image://../images/arrow/b_u_32.png',
            symbolSize: getScaSize,
            data: v.buyslclose,
          },
          {
            name: '空单开单',
            type: 'scatter',
            color: '#191970',
            symbolSize: getScaSize,
            data: v.sellopen,
          },
          {
            name: '空单止盈',
            type: 'scatter',
            symbol: 'image://../images/arrow/bl_d_32.png',
            symbolSize: getScaSize,
            data: v.selltpclose,
          },
          {
            name: '空单止损',
            type: 'scatter',
            symbol: 'image://../images/arrow/b_d_32.png',
            symbolSize: getScaSize,
            data: v.sellslclose,
          }
        ]
      }
      myChart.setOption(option);
      charts.push(myChart);
    });
    echarts.connect(charts);
  }

  app.controller('index', ['$scope', '$http','$location', '$filter','$window','$timeout',function ($scope, $http,$location,$filter,$window,$timeout) {

      //每个页面都要有的方法begin
      $scope.init = function(){
        $scope.wHeigth=$window.innerHeight;//获取屏幕的高度
        $scope.wWidth=$window.innerWidth;//获取屏幕的宽度
        var id = $location.search().id;
        if(id=='' || id==null || id=='undefined'){
           $scope.haserror = true;
           $scope.errormessage = '参数不正确，请从货币组页面点击编辑按钮进入';
           return;
        }
        $scope.id = id;
        getUser($http,$scope,'策略测试明细','策略管理');
      }

      $scope.logout = function(){
        logout($http);
      }

      $scope.menuclick=function(menu){
        changemenuclass($scope,menu);
      }

      $scope.redi=function(url){
          location.href = url;
      }
      //每个页面都要有的方法begin

      //每个页面都需要重写此方法，发生在init之后
      $scope.initAfter = function(){
        $scope.query();
      }

      $scope.query = function(){
        $scope.haserror = false;
        $http.get('quant4j-service/strategy_test/'+$scope.id).success(function(data,header,config,status) {
          if(data.ok){
            $scope.data = data.data;
            $scope.querySymbol();
            if(!data.data.isEnd){
              $timeout($scope.query,3000);
            }
          }else{
            $scope.haserror = true;
            $scope.errormessage = data.message;
          }
        }).error(function(data,header,config,status){
            $scope.haserror = true;
            $scope.errormessage = data.message;
        });
      }

      $scope.querySymbol = function(){
        $http.get('quant4j-service/strategy_test/'+$scope.id+'/symbols').success(function(data,header,config,status) {
          if(data.ok){
            $scope.symbols = data.data;
            $scope.queryChart();
          }else{
            $scope.haserror = true;
            $scope.errormessage = data.message;
          }
        }).error(function(data,header,config,status){
            $scope.haserror = true;
            $scope.errormessage = data.message;
        });
      }

      $scope.queryChart =function(){
        $http.get('quant4j-service/strategy_test/'+$scope.id+'/chart').success(function(data,header,config,status) {
          if(data.ok){
            $scope.chartdata = data.data;
            initchart(data.data);
          }else{
            $scope.haserror = true;
            $scope.errormessage = data.message;
          }
        }).error(function(data,header,config,status){
            $scope.haserror = true;
            $scope.errormessage = data.message;
        });
      }

      $scope.init();
  }]);
</script>
</html>